<?php

namespace app\models;

use think\Model;
use EasyWeChat\Factory;


class Order extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $updateTime = 'updated';

    public function getStatusTextAttr($value, $data)
    {
        $text = '';
        switch ($data['status']) {
            case '1':
                $text = '待付款';
                break;
            case '2':
                if ($data['type'] == 1) {
                    $text = '待配送';
                } else {
                    $text = '待取件';
                }
                break;
            case '3':
                if ($data['type'] == 1) {
                    $text = '配送中';
                } else {
                    $text = '取件中';
                }
                break;
            case '4':
                $text = '已完成';
                break;
            case '5':
                $text = '已取消';
                break;
            default:
                # code...
                break;
        }
        if ($data['tuikuan'] == 1) {
            $text = $text . '[处理中]';
        }
        if ($data['tuikuan'] == 2) {
            $text = $text . '[退款中]';
        }
        if ($data['tuikuan'] == 3) {
            $text = $text . '[商家拒绝]';
        }
        if ($data['tuikuan'] == 4) {
            $text = $text . '[退款失败]';
        }
        if ($data['tuikuan'] == 5) {
            $text = $text . '[已退款]';
        }
        return $text;
    }

    //通知提醒
    public function Message($openid = '', $template_id = '', $data = [], $appid, $pagepath)
    {
        $config = config('wechat2.');
        $app = Factory::officialAccount($config);
        $send_data = [
            'template_id' => $template_id, // 所需下发的订阅模板id
            'touser' => $openid,     // 接收者（用户）的 openid
            'miniprogram' => [
                'appid' => $appid,
                'pagepath' => $pagepath,
            ],
            'data' => $data
        ];
        $result = $app->template_message->send($send_data);
        return $result;
    }
}